package com.example.administrator.android01app;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Ch4Activity3 extends AppCompatActivity implements View.OnFocusChangeListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_ch4_3);

        EditText email=(EditText)findViewById(R.id.ch4_3_email);
        email.setOnFocusChangeListener(this);

        LinearLayout linearLayout=(LinearLayout)findViewById(R.id.ch4_3_ll);
        linearLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                TextView textView=(TextView)findViewById(R.id.ch4_3_tv);
                float x=motionEvent.getX();
                float y=motionEvent.getY();
                textView.setText("x:"+x+",y:"+y);
                //返回值代表是否处理完毕
                return true;
            }
        });
    }

    @Override
    public void onFocusChange(View view, boolean b) {
        //参数b,true时，代表获得焦点；false时，失去焦点
        if(!b){
            //失去焦点时，判断邮箱地址的规范性
            EditText editText=(EditText)view;
            String content=editText.getText().toString();

            //int pos=content.indexOf("@");//判断@的位置，如果为-1,则它不存在
            String regEx1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
            Pattern pattern=Pattern.compile(regEx1);
            Matcher matcher=pattern.matcher(content);

            TextView textView=(TextView)findViewById(R.id.ch4_3_tv);
            if(!matcher.matches()){
                //输出提示信息
                textView.setText("invalid email address");
            }else {
                //清空提示信息
                textView.setText("");
            }
        }
    }
}
